package com.web2.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.web2.utils.ConfigUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class LoginController {

    /**
     * 登录页
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/login")
    public String login(HttpServletRequest request) {
        return "login";
    }

    /**
     * 首页
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/index")
    public String index(HttpServletRequest request) {
        return "index";
    }

    /**
     * 登录请求，转发到SSO
     *
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/signIn", method = RequestMethod.POST)
    public void signIn(HttpServletRequest request, HttpServletResponse response) {
        String userName = request.getParameter("userName");
        String password = request.getParameter("passWd");
        StringBuilder url = new StringBuilder(ConfigUtils.getProperty("sso.url.signIn"));
        url.append("?returnUrl=http://web2.com/web2/index");
        url.append("&userName=").append(userName);
        url.append("&password=").append(password);
        response.addHeader("location", url.toString());
        response.setStatus(302);
    }
}
